﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	[Authorize]
	public class UserAvatarController : DefaultController
	{
		public UserAvatarController(IUserAvatarService userAvatarService)
		{
			this.UserAvatarService = userAvatarService;
		}

		public IUserAvatarService UserAvatarService
		{
			get; 
			set;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Index(int? page)
		{
			var avatars = UserAvatarService.GetPaged(new UserAvatarSpecification
			{
				UserId = Identity.Id,
				Page = page,
				Limit = Settings.Users.AvatarPageLimit
			});

			var avatar = avatars.FirstOrDefault();

			return avatar.CanView ? View(Views.Index, avatars) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Create()
		{
			var avatar = UserAvatarService.Create();

			return avatar.CanCreate ? View(Views.Create, new UserAvatarCreate()) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken]
		public ActionResult Create(UserAvatarCreate value)
		{
			try
			{
				var avatar = UserAvatarService.Create();

				if (!avatar.CanCreate)
				{
					return NotAuthorized();
				}

				value.Source = Request.Files[0];

				this.UserAvatarService.Insert(avatar, value);

				return Message(Routes.UserAvatarIndex, new { page = 1 }, Resources.Messages.UserAvatarCreated);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Select(int id)
		{
			var avatar = UserAvatarService.GetById(id);

			if (avatar == null)
			{
				return Error(Resources.Messages.UserAvatarNotFound);
			}

			if (!avatar.CanUpdate)
			{
				return NotAuthorized();
			}

			UserAvatarService.Select(avatar);

			return RedirectToRoute(Routes.UserAvatarIndex, new { page = 1 });
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Delete(int id)
		{
			var avatar = UserAvatarService.GetById(id);

			if (avatar == null)
			{
				return Error(Resources.Messages.UserAvatarNotFound);
			}

			if (!avatar.CanDelete)
			{
				return NotAuthorized();
			}

			UserAvatarService.Delete(avatar);

			return RedirectToRoute(Routes.UserAvatarIndex, new { page = 1 });
		}
	}
}